#!/usr/bin/env python
#
#       Data_group.py
#       
#       Copyright 2009 Brandon Invergo <brandon@brandoninvergo.com>
#       
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

import random

class Data_group:
    def add_member(self, member_name, member_id, member_role, member_aux, n):
        #member = dict({"name" : member_name, "role" : member_role, "aux" : member_aux})    
        member = Data_object(member_name, member_id, member_role, member_aux, n)
        self.remove_member(member_name)
        self.member_list.append(member)
        role_count = self.member_dict.get(member_role)
        if role_count == None:
            self.member_dict[member_role] = 1
        else:
            self.member_dict[member_role] = role_count + 1
            
    def remove_member(self, member_name):
        for member in self.member_list:
            if member.name == member_name:
                self.member_list.remove(member)
                role = member.role
                role_count = self.member_dict.get(role)
                role_count = role_count - 1
                if role_count > 0:
                    self.member_dict[role] = role_count
                else:
                    del self.member_dict[role]
                return True
        return False
        
    def clear_dict(self):
        self.member_dict.clear()
        
    def add_object(self, object, n):
        obj_role = object.split("..")[1]
        obj_role = obj_role.replace(".", " ")
        obj_name = " ".join([self.name, obj_role])
        self.add_member(obj_name, object, obj_role.title(), "", n)
        return obj_name
        
    def clear_members(self):
        self.member_list = []
    
    def add_result(self, analysis_name, command, result_text):
        result_dict = {"analysis" : analysis_name, "command" : command, \
            "result" : result_text, "notes" : "Notes for %s - %s\n--------\n"%(self.name, analysis_name)}
        self.results_list.append(result_dict)
        
    def edit_result(self, analysis_name, command = None, results_text = None, notes_text = None):
        for result in self.results_list:
            if result.get("analysis") == analysis_name:
                if command:
                    result["command"] = command
                if results_text:
                    result["result"] = results_text
                if notes_text:
                    result["notes"] = notes_text
                return True
        return False
    
    def has_result(self, analysis_name):
        for result in self.results_list:
            if result.get("analysis") == analysis_name:
                return True
        return False
        
    def get_result(self, analysis_name):
        for result in self.results_list:
            if result.get("analysis") == analysis_name:
                return result
        return None
                
    def remove_result(self, analysis_name):
        for result in self.results_list:
            if result.get("analysis") == analysis_name:
                self.results_list.remove(result)
                return True
        return False
    
    def __init__(self, name):
        self.member_dict = {}
        self.member_list = []
        self.results_list = []
        self.name = name
        id_num = random.randint(1, 999999)
        self.id_str = str(id_num).join(["dg", ".."])
        
class Data_object:
    def __init__(self, name, id, role, aux, n):
        self.name = name
        self.id = id
        self.role = role
        self.aux = aux
        self.n = n
